// pages/teacher-register/index.js
const { $Message } = require('../../dist/base/index');
const handler = require("../../utils/function.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendtxt: "获取验证码",
    phone: null,
    phoneTest: false,
    verificationcode: null,
    verificationcodeTest: false,
    gettingCode: true,
    password: null,
    passwordTest: false,
    copypassword:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  // phoneinput 获取电话号码
  phoneinput: function (event) {
    var that = this
    that.setData({
      phone: event.detail.value
    })
    this.phoneTest(event.detail.value)
  },
  // phoneTest 电话号码验证
  phoneTest: function (params) {
    if (params == '' || !/^1\d{10}$/.test(params)) {
      wx.showToast({
        title: '请输入正确的电话号码格式',
        icon: "none"
      })
      this.setData({
        phoneTest: false,
      });
      return;
    } else {
      this.setData({
        phoneTest: true,
      });
    }
  },
  // 发送验证码
  sendcode: function () {
    var that = this;
    this.phoneTest(this.data.phone)
    // console.log('触发发验证码')
    handler.requestData("sendSMS", { phone: that.data.phone }, function (res) {
      if (res.success == true) {
        that.countDown()
        $Message({
          content: '验证码发送成功,有效期2小时',
          type: 'success'
        });
      } else {
        $Message({
          content: res.message,
          type: 'error'
        });
      }
    });
    // wx.request({
    //   url: 'https://product.fishqc.com/Api/WeixinLogin/weixinminiMobileVerificationCode',
    //   data: {
    //     mobile: that.data.phone
    //   },
    //   method: 'POST',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded' // 默认值
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     if (res.data.code == 200) {
    //       that.countDown()
    //     } else {
    //       wx.showToast({
    //         title: '获取验证码失败',
    //         icon: "none"
    //       })
    //     }
    //   },
    //   fail: function () {
    //     wx.showToast({
    //       title: '获取验证码失败',
    //       icon: "none"
    //     })
    //   }
    // })
  },

  //倒计时 倒数
  countDown: function () {
    var that = this;
    that.data.gettingCode = false;
    var seconds = 59;
    var countTimer = setInterval(function () {
      that.setData({
        sendtxt: seconds + "s"
      })
      seconds--;
      if (seconds <= 0) {
        countTimer = clearInterval(countTimer);
        that.setData({
          sendtxt: '获取验证码'
        })
        that.data.gettingCode = true;
      }
    }, 1000);
  },
  verificationinput: function (e) {
    this.setData({
      verificationcode: e.detail.value,
    });
    this.verificationTest(e.detail.value);
  },
  verificationTest: function (params) {
    if (params == null) {
      wx.showToast({
        title: '请输入验证码',
        icon: "none"
      })
      return;
    } else {
      this.setData({
        verificationcodeTest: true,
      });
    }
  },
  // passwordinput 获取密码
  passwordinput: function (e) {
    console.log(e.detail.value);
    this.setData({
      password: e.detail.value
    });
    this.passwordTest(e.detail.value);
  },
  // passwordTest 密码验证
  passwordTest: function (params) {
    if (params == '' || !/^.{6,}$/.test(params)) {
      wx.showToast({
        title: '请输入正确的密码格式',
        icon: "none"
      })
      return;
    } else {
      this.setData({
        passwordTest: true,
      });
    }
  },
  // 重复新密码
  copypasswordinput:function(e){
    const password=this.data.password;
    const copypassword=e.detail.value;
    console.log(e.detail.value);
    if (password!==copypassword){
      handler.showToast("两次密码输入不一致，请重新输入");
      this.setData({ copypassword: false });
    }else{
      this.setData({ copypassword:true});
    }
  },
  // getPhoneLogin 登录的电话号码
  getPhoneLogin: function () {
    const {
      sendtxt,
      phone,
      phoneTest,
      verificationcode,
      verificationcodeTest,
      password,
      passwordTest,
      copypassword
    } = this.data;
    var user_type = 0;
    var temp_user_type = wx.getStorageSync('user_type');
    if (temp_user_type == 'teacher') {
      user_type = 1
    } else {
      user_type = 0;
    }
    var submitData = {
      "phone": phone,
      "code": verificationcode,
      "password": password,
      "confirmPassword": copypassword,//true两次密码一致false不一致
      "user_type": user_type,
      "id": wx.getStorageSync('user_info')['id']
    };
    
    if (verificationcodeTest && passwordTest && phoneTest && copypassword) {
      var that = this;
      handler.requestData("tearchRegister", submitData, function (res) {
        if (res.success == true) {
          $Message({
            content: '注册成功',
            type: 'success'
          });
          handler.intoPageHandler("../login/index?type=" + wx.getStorageSync('user_type'));
        } else {
          wx.showToast({
            title: res.message,
            icon: 'loading'
          })
        }
      });
      
    } else {
      $Message({
        content: '注册失败',
        type: 'error'
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})